// pages/shouye/shouye.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'https://img23.iblimg.com/market-2/images/activity/1683008182.jpg',
      'https://img23.iblimg.com/market-2/images/activity/713634736.jpg',
      'https://img23.iblimg.com/market-2/images/activity/442493220.jpg',
      'https://img22.iblimg.com/market-2/images/activity/335073144.jpg',
      'https://img23.iblimg.com/market-2/images/activity/971746933.jpg',
      'https://img22.iblimg.com/market-2/images/activity/63058819.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    items: 'soukuang',
    arr: [],
    brr: [],
    show: true,
    add: false,
    is: "addon",
    ises: "",
    pageing: {
      currentpage: 1,
      pagesize: 2,
      total: 0
    },
    totalpage: 0,
    ifis: false,
    loading: '加载中...',
  },
  imgbtn(e){
    wx.setStorage({
      key: 'name',
      data: e.currentTarget.dataset.item,
    })
    wx.navigateTo({
      url: '../xiangqing/xiangqing',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: 'i 百联',
    })
  },
  onPageScroll:function(e){
    if(e.scrollTop>0){
      this.setData({
        items: 'soukuang'
      })
    }
  },
  pintuanbtn() {
    this.setData({
      show: true,
      add: false,
      is: "addon",
      ises: ''
    })
  },
  kanjiabtn() {
    this.setData({
      show: false,
      add: true,
      is: "",
      ises: "addon"
    })
  },
  gettotal(total) {
    this.setData({
      totalpage: Math.ceil(total / this.data.pageing.pagesize)
    })
  },
  getpage(cb) {
    let {
      currentpage,
      pagesize,

    } = this.data.pageing, total
    if (this.data.totalpage != 0 && this.data.pageing.currentpage == this.data.totalpage) {
      this.setData({
        ifis: true,
        loading: '没有更多数据'
      })
    
    }
    wx.showLoading({
      title: '加载中...',
    })
    wx: wx.request({
      url: 'http://localhost/api.php/bailian/phonelist',
      method: "get",
      data: {
        transform: 1,
        order: "id,desc",
        page: `${currentpage},${pagesize}`
      },
      success: res => {
        
        this.setData({
          arr: [...this.data.arr, ...res.data.phonelist],
          total: res.data._results
        })
        this.gettotal(this.data.total)
        wx.hideLoading()
        cb && cb()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getdata()
    this.getpage()
  },
  getdata() {
    wx: wx.request({
      url: 'http://localhost/api.php/bailian/tuanlist',
      method: "get",
      data: {
        transform: 1,
        order: "id,desc"
      },
      success: res => {
        this.setData({
          brr: res.data.tuanlist
        })
        console.log(res.data.tuanlist)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      items: 'on',
      arr: [],
      pageing: {
        ...this.data.pageing,
        currentpage: 1
      }
    })
    this.getpage(function() {
      wx.stopPullDownRefresh()
    })
    console.log(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      pageing: {
        ...this.data.pageing,
        currentpage: ++this.data.pageing.currentpage

      }
    })
    this.getpage()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})